/*
 * GraphWindow.java
 *
 * Created on May 12, 2006, 10:44 AM
 *
 * @author  Francois Daniels
 */

import java.awt.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class GraphWindow extends javax.swing.JFrame {
    /** Creates new form GraphWindow */
    ChartPanel chartPanel;
    JFreeChart graph;
    ConfigurationInterface parent;
    
    public GraphWindow () {
        initComponents ();
        updateComponents ();
        parent = null;
    }
    
    public GraphWindow (ConfigurationInterface parent) {
        this.parent = parent;
        initComponents ();
        updateComponents ();
    }
    
    public GraphWindow (ConfigurationInterface parent, String title, 
            String range, String domain, int [] range_data, 
            String [] domain_data) {
        
        this.parent = parent;
        initComponents ();
        
        if (range_data == null || domain_data == null) return;
        
        graph = getBarChart (title, range, domain, range_data, 
                domain_data);
        chartPanel = new ChartPanel (graph);
        getContentPane ().add (chartPanel, 
                java.awt.BorderLayout.CENTER);
        centerOnParentAndShow ();
    }
    
    private void updateComponents () {
        graph = getPieChart ();
        chartPanel = new ChartPanel (graph);
        getContentPane ().add (chartPanel, 
                java.awt.BorderLayout.CENTER);
        centerOnParentAndShow ();
    }
    
    private void centerOnParentAndShow () {
        if (parent == null) return;
        
        Dimension dparent = parent.getSize ();
        Dimension window = getSize ();
        int xCoord, yCoord;
        try {
            xCoord = parent.getLocationOnScreen ().x + 
                    (dparent.width/2 - window.width/2);
            yCoord = parent.getLocationOnScreen ().y + 
                    (dparent.height/2 - window.height/2);
        } catch (IllegalComponentStateException e) {
            return;}
        
        //Ensure that no part of the window will be off-screen
        Dimension screen = Toolkit.getDefaultToolkit ().
                getScreenSize ();
        int xOffScreenExcess = xCoord + window.width - screen.width;
        if ( xOffScreenExcess > 0 ) {
            xCoord = xCoord - xOffScreenExcess;
        }
        if (xCoord < 0 ) {
            xCoord = 0;
        }
        int yOffScreenExcess = yCoord + window.height - screen.height;
        if ( yOffScreenExcess > 0 ) {
            yCoord = yCoord - yOffScreenExcess;
        }
        if (yCoord < 0) {
            yCoord = 0;
        }
        
        setLocation ( xCoord, yCoord );
    }
    
    public JFreeChart getBarChart (String title, String range, 
            String domain, int [] range_data, String [] domain_data) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset ();
        int len = (range_data.length > domain_data.length)?
            domain_data.length:range_data.length;
        
        for (int i = 0; i < len; i++)
            dataset.setValue (range_data [i], range, domain_data [i]);
        
        return ChartFactory.createBarChart (title,
                domain, range, dataset,
                PlotOrientation.VERTICAL, false, true, false);
    }
    
    /**
     * Gets an example piechart.
     * @return a piechart
     */
    public JFreeChart getPieChart () {
        DefaultPieDataset dataset = new DefaultPieDataset ();
        dataset.setValue ("January", 60);
        dataset.setValue ("February", 10);
        dataset.setValue ("March", 30);
        JFreeChart graph = ChartFactory.createPieChart3D (
                "Total Calls by Month",
                dataset,
                true,
                true,
                false);
        PiePlot3D plot = (PiePlot3D) graph.getPlot ();
        plot.setForegroundAlpha ( 0.6f );
        plot.setStartAngle (270);
        plot.setDirection (org.jfree.util.Rotation.ANTICLOCKWISE);
        final Rotator rotator = new Rotator (plot);
        rotator.start ();
        return graph;
    }
    
    /**
     * Gets an example XY chart
     * @return an XY chart
     */
    public JFreeChart getXYChart () {
        XYSeries series = new XYSeries ("XYGraph");
        series.add (1, 5);
        series.add (2, 7);
        series.add (3, 3);
        series.add (4, 5);
        series.add (5, 4);
        series.add (6, 5);
        XYSeriesCollection dataset = new XYSeriesCollection ();
        dataset.addSeries (series);
        return ChartFactory.createXYLineChart (
                "XY Chart", "X-axis", "Y-axis", dataset,
                PlotOrientation.VERTICAL, true, true, false);
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Code generated by Netbeans for the GUI Components  ">
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Graph");
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-640)/2, (screenSize.height-480)/2, 640, 480);
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main (String args[]) {
        java.awt.EventQueue.invokeLater (new Runnable () {
            public void run () {
                new GraphWindow ().setVisible (true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}
